package com.mms.error;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Component
public class MyHandLerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ex.printStackTrace();
        FastJsonJsonView jsonView = new FastJsonJsonView();
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("code", -1);
        stringObjectMap.put("msg", "异常");
        jsonView.setAttributesMap(stringObjectMap);
        ModelAndView mv = new ModelAndView();
        mv.setView(jsonView);
        return mv;
    }
}
